package com.android.xunxian;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.activity.DeviceActivity;
import com.android.xunxian.activity.LoginActivity;
import com.android.xunxian.activity.SaveServiceActivity;
import com.android.xunxian.activity.SettingActivity;
import com.android.xunxian.activity.UpdateXunxActivity;
import com.android.xunxian.activity.XunxActivity1;
import com.android.xunxian.activity.XunxMapActivity;
import com.android.xunxian.activity.XunxPlanActivity;
import com.android.xunxian.utils.UConstants;
import com.android.xunxian.utils.Util;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zouqh on 2016/7/23.
 */
public class HomeActivity extends Activity implements View.OnClickListener {
    public static HomeActivity mainActivity;
    @InjectView(R.id.layout1)
    LinearLayout layout1;
    @InjectView(R.id.layout2)
    LinearLayout layout2;
    @InjectView(R.id.layout3)
    LinearLayout layout3;
    @InjectView(R.id.layout4)
    LinearLayout layout4;
    @InjectView(R.id.layout5)
    LinearLayout layout5;
    @InjectView(R.id.layout6)
    LinearLayout layout6;
    @InjectView(R.id.userName)
    TextView userName;
    @InjectView(R.id.bumen)
    TextView bumen;
    @InjectView(R.id.comName)
    TextView comName;

    Util util;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);
        mainActivity = this;
        ButterKnife.inject(this);
        layout1.setOnClickListener(this);
        layout2.setOnClickListener(this);
        layout3.setOnClickListener(this);
        layout4.setOnClickListener(this);
        layout5.setOnClickListener(this);
        layout6.setOnClickListener(this);
        userName.setText("姓名："+UConstants.EmpName);
        bumen.setText("部门："+UConstants.DepName);
        comName.setText(UConstants.ComName);
        util = new Util(this);
        String name = util.getUser("time");
        if (Util.NoNull(name)){
            UConstants.TIME=Integer.parseInt(name);
        }else{
            UConstants.TIME=60;
        }

    }


    @Override
    public void onClick(View v) {

        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.layout1:
                intent.setClass(this, XunxMapActivity.class);
                startActivity(intent);
                break;
            case R.id.layout2:
                intent.setClass(this, XunxActivity1.class);
                startActivity(intent);
                break;
            case R.id.layout3:
                intent.setClass(this, XunxPlanActivity.class);

                startActivity(intent);
                break;
            case R.id.layout4:
                intent.setClass(this, DeviceActivity.class);
                startActivity(intent);
                break;

            case R.id.layout5:
                intent.setClass(this, SettingActivity.class);

                startActivity(intent);
                break;
            case R.id.layout6:
                new Util(this).setUser(UConstants.EmpId, "", "0");
                intent.setClass(this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;

        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (keyCode == KeyEvent.KEYCODE_BACK
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    Toast.makeText(getApplicationContext(), "再按一次退出程序",
                            Toast.LENGTH_SHORT).show();
                    exitTime = System.currentTimeMillis();
                } else {
                    finish();
                    System.exit(0);
                }
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
        return true;
    }
}
